<template>
  <div>
    <el-table
      ref="singleTable"
      :disable="disabled"
      :data="tableData"
      :header-row-style="{'line-height':'20px'}"
      border
      highlight-current-row
      style="width: 100%;">
      <el-table-column
        type="index"
        width="80">
      </el-table-column>
      <el-table-column
        v-for="(item, index) in tableList"
        :property="item.prop"
        :label="item.label"
        :key="index"
        width="150">
        <template
          slot-scope="scope">
          <el-select v-if="item.prop == 'typeVue'" v-model="scope.row.typeVue" :disabled="disabled" filterable placeholder="请选择" @change = selectChange(scope)>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-input v-else v-model="scope.row.mobileNum" :disabled="disabled" @input="valueChange" placeholder="请输入内容"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="120"
        style="line-height: 20px">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="deleteRow(scope.$index, tableData)"
            type="text"
            size="small"
            :disabled="disabled ? disabled : scope.$index === 0">
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <span @click="addTypeAndMoblie" class="addTypeAndMob" :class="{'disAdd': disabled}">+添加</span>
  </div>
</template>

<script>
export default {
  name: 'typeAndmobile',
  props: {
    value: {
      type: [String, Number, Array],
      default: ''
    },
    /** 索引值 用于更新数据 */
    index: Number,
    /** 包含数据源 */
    item: Object,
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: '',
      options: [{
        value: '工作',
        label: '工作'
      }, {
        value: '手机',
        label: '手机'
      }, {
        value: '住址',
        label: '住址'
      }, {
        value: '主要',
        label: '主要'
      }, {
        value: '传真',
        label: '传真'
      }],
      tableList: [
        { prop: 'typeVue',
          label: '类型'
        },
        { prop: 'mobileNum',
          label: '电话'
        }
      ]
    }
  },
  watch: {
    value: {
      immediate: true,
      handler: function(val) {
        if (val === '') {
          this.tableData = [{
            typeVue: '',
            mobileNum: ''
          }]
        } else this.tableData = val
      }
    }
  },
  mounted() {
  },
  methods: {
    selectChange(item) {
      this.$emit('value-change', {
        index: this.index,
        value: this.tableData
      })
    },
    valueChange(val) {
      const data = {
        index: this.index,
        value: this.tableData
      }
      this.$emit('value-change', data)
    },
    addTypeAndMoblie() {
      if (this.disabled) {
        return
      } else {
        this.tableData.push({
          typeVue: '',
          mobileNum: '' })
      }
    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
    }
  }
}
</script>

<style scoped>
.addTypeAndMob{
  color: #3c8be3;
  cursor: pointer;
}
.disAdd{
  color: #C0C4CC;
  cursor: not-allowed;
}
.deletedMoblie{
  width: 35px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  border-radius: 50%;
  background-color: #f94e4e;
  font-size: 32px;
  color: #ffffff;
}
</style>
